'use strict';
var gulp = require('gulp'),
    banner = require('gulp-banner'),            // 文件顶部添加说明
    cdnify = require('gulp-cdnify'),            // cdn路径替换
    clean = require('gulp-clean'),              // 清空目录
    cleanCss = require('gulp-clean-css'),       // 压缩css
    htmlmin = require('gulp-htmlmin'),          // 压缩html
    jshint = require('gulp-jshint'),            // js检测
    less = require('gulp-less'),                // less编译
    livereload = require('gulp-livereload'),    // 监听文件改变并刷新
    rev = require('gulp-rev'),                  // 添加md5戳
    sequence = require('gulp-sequence'),        // 串行并行执行
    uglify = require('gulp-uglify'),            // 压缩js
    usemin = require('gulp-usemin'),            // 页面css、js资源优化合并
    webserver = require('gulp-webserver'),      // 本地服务器
    babel = require('gulp-babel');              // 编译 es6 语法

var port = 8000; // 监听端口

// var cdnPath = '//s.thsi.cn/cd/mobileweb-vaserviece-limit-up-api/limitUp/'; // cdn路径
var cdnPath = '//s.thsi.cn/cd/mobileweb-vaserviece-limitup-front-container/'; // cdn路径

var bannerTxt = '/* @charset "utf-8";bower by hexin; */'; // 文件顶部添加说明

// 服务启动，监听
gulp.task('webserver', function() {
    return gulp.src('./dist')
               .pipe(webserver({
                    host: 'localhost',
                    port: port,
                    livereload: true,
                    directoryListening: true,
                    open: true
               }));
});

gulp.task('watch', function() {
    gulp.watch('./app/**', ['copy']);
});

// 清空dist下的文件
gulp.task('clean', function() {
    var cleanSrc = './dist/*';
    
    return gulp.src(cleanSrc, {read : false})
               .pipe(clean());
});

// 清空css,js,images,html等文件
gulp.task('cleanfiles', function() {
    var src = ['./dist/images/**.{gif,jpeg,jpg,png}','./dist/styles/{,*/}*.{css,less}','./dist/scripts/{,*/}*.js','./dist/*.html'];

    return gulp.src(src, {read: false})
           .pipe(clean());
});

// 拷贝所有文件
gulp.task('copy', function() {
    var src = './app/**',
        des = './dist';

    return gulp.src(src)
               .pipe(gulp.dest(des));
});

// 图片压缩
gulp.task('imagecopy', function() {
    var imageSrc = './app/images/*.{gif,jpeg,jpg,png}',
        imageDes = './dist/images';

    return gulp.src(imageSrc)
               .pipe(gulp.dest(imageDes));
});

// 合并html中css，js文件
gulp.task('usemin', function() {
    return gulp.src('./app/*.html')
               .pipe(usemin({
                    css: [ less, cleanCss, rev],
                    html: [ function () { return htmlmin({ collapseWhitespace: true }); }],
                    js: [ babel, function(){return uglify({preserveComments:'license'})}, rev ],
               }))
               .pipe(gulp.dest('./dist/'));
});

// 替换页面资源cdn路径
gulp.task('cdnify', function() {
    return gulp.src(['./dist/**/*.html'])
               .pipe(cdnify({
                    base: cdnPath
               }))
               .pipe(gulp.dest('./dist'));
});

// 文件顶部添加说明
gulp.task('banner', function () {
    return gulp.src('./dist/**/*.{css,js}')
               .pipe(banner(bannerTxt))
               .pipe(gulp.dest('./dist'));
});

// jshint检测
gulp.task('jshint', function() {
    return gulp.src('./app/scripts/*.js')
               .pipe(jshint({lookup:false}))
               .pipe(jshint.reporter('jshint-stylish'));
});


// 入口
// 启动页面-server
gulp.task('server', sequence('clean', 'jshint', 'copy', 'watch', 'webserver'));

// 测试环境打包(压缩文件)
gulp.task('default', sequence('clean', 'copy', 'cleanfiles', ['usemin', 'imagecopy'], 'cdnify', 'banner'));

// 测试环境打包(非压缩)
gulp.task('build', sequence('clean', 'jshint', 'copy', 'cdnify', 'banner'));

